package com.shelpe.services.auth.controller;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.shelpe.services.auth.entity.SubwayToken;
import com.shelpe.services.auth.entity.Users;
import com.shelpe.services.auth.repository.SubwayTokenRepository;
import com.shelpe.services.auth.repository.UsersRepository;
import com.shelpe.services.auth.service.TokenService;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/taobao")
public class GatewayController {

	@Autowired
	private TokenService tokenService;
	@Autowired
	private SubwayTokenRepository subwayTokenRepository;
	@Autowired
	private UsersRepository usersRepository;

	/**
	 * 获取accessToken
	 * 
	 * @param nick
	 * @return
	 */
	@RequestMapping("/accessTokens/{idenity}")
	public @ResponseBody String getToken(@PathVariable("idenity") String idenity) {
		UserToken token = this.findUserToken(idenity);
		return token!=null?token.getAccessToken():null;
	}
	
	@RequestMapping("users/{idenity}/accessTokens")
	public @ResponseBody UserToken findUserToken(@PathVariable("idenity") String idenity) {
		UserToken token = new UserToken();
		
		TokenService.UserInfo info = null;
		if(NumberUtils.isNumber(idenity)){
			long userId = 0;
			userId = NumberUtils.toLong(idenity);
			info = tokenService.getToken(userId);
		}else{
			info = tokenService.getToken(idenity);
		}
		
		if(info != null && info.getAuth() != null){
			token.setAccessToken(info.getAuth().getAccessToken());
			if(info.getUser() != null){
				if(info.getUser().getUserType() == Users.USER_TYPE_PRIMARY){
					token.setType(UserToken.TYPE_PRIMARY);
				}else if(info.getUser().getUserType() == Users.USER_TYPE_PROXY){
					token.setType(UserToken.TYPE_PROXY);
				}else{
					token.setType(UserToken.TYPE_PURCHASE);
				}
			}else{
				token.setType(UserToken.TYPE_PURCHASE);
			}
		}else{
			log.error("fail to find token: {}", idenity);
		}
		
		return token;
	}
	
	@Data
	@JsonInclude(Include.NON_NULL)
	public static class UserToken{
		public final static String TYPE_PROXY = "proxy";
		public final static String TYPE_PURCHASE = "purchase";
		public final static String TYPE_PRIMARY = "primary";
		
		@JsonProperty("access_token")
		private String accessToken;
		private String type;//proxy or purchase
	}

	@RequestMapping("/subwayTokens/{idenity}")
	@ResponseBody
	public String subwayToken(@PathVariable("idenity") String idenity) {
		long userID = 0;
		if (NumberUtils.isNumber(idenity)) {
			userID = NumberUtils.toLong(idenity);
		}else{
			Users user = usersRepository.findTopByNickOrderByUserIdDesc(idenity);
			if (user != null) {
				userID = user.getUserId();
			}
		}
		SubwayToken token = null;
		if (userID != 0) {
			token = this.subwayTokenRepository.findOne(userID);
		}else{
			log.error("fail to find user id: {}", idenity);
		}
		
		if(token == null){
			log.error("fail to find subway token: {}", idenity);
		}
		
		return token != null ? token.getSubwayToken() : null;
	}
	
}
